from .db import LocalDB_JSON


class Color:
    Red = "\033[31m"
    Blue = "\033[34m"
    Green = "\033[32m"
    Yellow = "\033[33m"
    Clear = "\033[0m"


def get_unpacked_user(idx=None):
    djb = LocalDB_JSON()
    user = djb.get_user(idx)
    return user["api_id"], user["api_hash"], user["file_session"]


def append_user():
    print("For append first send me api id\n>>")
    api_id = input()
    print("Now give me api hash\n>> ")
    api_hash = input()
    djb = LocalDB_JSON()
    djb.create_user(api_id, api_hash)


def create_session():
    import os
    import telethon

    djb = LocalDB_JSON()
    users = djb.get_users()
    counter = 0
    for user in users:
        if os.path.exists(os.path.join(os.getcwd(), user["file_session"])):
            continue
        counter += 1
        client = telethon.TelegramClient(
            user["file_session"], user["api_id"], user["api_hash"]
        )
        client.start()
        if os.path.exists(os.path.join(os.getcwd(), user["file_session"])):
            print(f"Successful login with {user['file_session']}")
        else:
            print(f'Cancel login with {user["file_session"]}')
    print(f"Finish, all accounts added {counter}")
